var amapFile = require('../../libs/amap-wx.130.js');

Page({
  data: {
    markers: [],
    distance: '',
    cost: '',
    polyline: [],
    latitude:'',
    longitude:''
  },
  onLoad: function() {
    var that=this
    var myAmapFun = new amapFile.AMapWX({key: '038c54183637f97bf87484c0ce1c7b01'});
    that.setData({
      markers:[
        {
          id: 0,
          latitude: getApp().globalData.origina,
          longitude: getApp().globalData.originb,
          width: 23,
          height: 33
        },{
          id: 1,
          latitude: getApp().globalData.desa,
          longitude: getApp().globalData.desb,
          width: 24,
          height: 34
        }
      ],
      latitude:getApp().globalData.origina,
      longitude:getApp().globalData.originb
    })
    let origin=getApp().globalData.originb+','+getApp().globalData.origina
    let des=getApp().globalData.desb+','+getApp().globalData.desa    
    console.log(origin)
    console.log(des)
    myAmapFun.getWalkingRoute({
      origin: origin,
      destination: des,
      success: function(data){
        console.log(data)
        var points = [];
        if(data.paths && data.paths[0] && data.paths[0].steps){
          var steps = data.paths[0].steps;
          for(var i = 0; i < steps.length; i++){
            var poLen = steps[i].polyline.split(';');
            for(var j = 0;j < poLen.length; j++){
              points.push({
                longitude: parseFloat(poLen[j].split(',')[0]),
                latitude: parseFloat(poLen[j].split(',')[1])
              })
            } 
          }
        }
        that.setData({
          polyline: [{
            points: points,
            color: "#0091ff",
            width: 6
          }]
        });
        if(data.paths[0] && data.paths[0].distance){
          that.setData({
            distance: data.paths[0].distance + '米'
          });
        }
        if(data.paths[0] && data.paths[0].duration){
          that.setData({
            cost: parseInt(data.paths[0].duration/60) + '分钟'
          });
        }
          
      },
      fail: function(info){
        console.log('1111')
      }
    })
  },
  goDetail: function(){
    wx.navigateTo({
      url: '../walkdetail/walkdetail'
    })
  },
  goToCar: function (e) {
    wx.redirectTo({
      url: '../321/321'
    })
  },
  goToBus: function (e) {
    if(getApp().globalData.descity==getApp().globalData.origincity){
      wx.redirectTo({
        url: '../bus/bus?'
      })
    }
    else{
      wx.showModal({
        cancelColor: 'cancelColor',
        title:'仅支持同城公交路线规划'
      })
    }
  },
  goToRide: function (e) {
    let distance=this.data.distance.substring(0,this.data.distance.length-1)

    if(distance<=500000){
      wx.redirectTo({
        url: '../ride/ride'
      })
    }
    else{
      wx.showModal({
        cancelColor: 'cancelColor',
        title: '距离较远不建议骑行'
      })
    }
  },
  goToWalk: function (e) {
    let distance=this.data.distance.substring(0,this.data.distance.length-1)

    if(distance<=100000){
      wx.redirectTo({
        url: '../walk/walk?'
      })
    }
    else{
      wx.showModal({
        cancelColor: 'cancelColor',
        title: '距离较远不建议步行'
      })
    }
  }
})